import 'package:cloud_medicine/components/shop_grade.dart';
import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/http/config.dart';
import 'package:cloud_medicine/http/shop_api.dart';
import 'package:cloud_medicine/model/shop_list.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class MedicineEditAndAdd extends StatefulWidget {
  final TextEditingController nameController;
  final TextEditingController priceController;
  final TextEditingController describeController;
  final TextEditingController numberController;
  final Function saveInfoAndCommit;
  final num id; //1为编辑  2为添加
  final String oldname;
  final String oldprice;
  final String olddescribe;
  final num oldnumber;
  final num medicineid;

  MedicineEditAndAdd(
      {Key key,
      @required this.nameController,
      @required this.priceController,
      @required this.describeController,
      @required this.saveInfoAndCommit,
      @required this.oldname,
      @required this.oldprice,
      @required this.olddescribe,
      @required this.numberController,
      this.oldnumber,
      @required this.id,
      this.medicineid})
      : super(key: key);

  @override
  _MedicineEditAndAddState createState() => _MedicineEditAndAddState();
}

class _MedicineEditAndAddState extends State<MedicineEditAndAdd> {
  String kind;
  bool identity;
  @override
  void initState() {
    super.initState();
    widget.priceController.text = widget.oldprice;
    widget.nameController.text = widget.oldname;
    widget.describeController.text = widget.olddescribe;
    widget.numberController.text = widget.oldnumber.toString();
  }

  Future _editMedicine() async {
    await editMedicine_api('$backIP/shopManage/alterMedicine', {
      'id': widget.medicineid,
      'name': widget.nameController.text,
      'picture': medicineImgUp,
      'see': widget.describeController.text,
      'price': double.parse(widget.priceController.text),
      'identity': identity ?? true,
      'kind': kind ?? '2',
      'repository_num': int.parse(widget.numberController.text)
    }).then((value) => medicineImgUp.clear());
  }

  Future _addMedicine() async {
    await addMedicine_api('$backIP/shopManage/addMedicine', {
      'name': widget.nameController.text,
      'picture': medicineImgUp,
      'see': widget.describeController.text,
      'price': double.parse(widget.priceController.text),
      'identity': identity ?? true,
      'kind': kind ?? '2',
      'repository_num': int.parse(widget.numberController.text)
    }).then((value) => {medicineImgUp.clear(), print(medicineImgUp)});
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          margin: EdgeInsets.all(10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TextField(
                controller: widget.nameController,
                decoration: InputDecoration(labelText: '药品名'),
              ),
              TextField(
                inputFormatters: [
                  FilteringTextInputFormatter(RegExp("[0.0-9.0]"), allow: true)
                ],
                controller: widget.priceController,
                decoration: InputDecoration(labelText: '价格,例如:24.8'),
              ),
              TextField(
                inputFormatters: [
                  FilteringTextInputFormatter(RegExp("[0-9]"), allow: true)
                ],
                controller: widget.numberController,
                decoration: InputDecoration(labelText: '库存数量,例如:46'),
              ),
              TextField(
                controller: widget.describeController,
                decoration: InputDecoration(labelText: '描述'),
              ),
              MedicineKindItem(
                getkindValue: (value) {
                  kind = value.toString();
                },
              ),
              MedicineIdentity(getidentityValue: (value) {
                if (value == 0) {
                  identity = true;
                } else {
                  identity = false;
                }
                print(identity);
              }),
              Container(
                margin: EdgeInsets.only(left: 20, right: 20, top: 30),
                width: double.infinity,
                height: 40,
                decoration: BoxDecoration(
                    gradient: LinearGradient(
                        colors: [Colors.lightBlueAccent, Colors.blueAccent]),
                    borderRadius: BorderRadius.circular(20)),
                child: RaisedButton(
                  child: Text(
                    '保存',
                    style: TextStyle(
                        color: Colors.white, fontWeight: FontWeight.w100),
                  ),
                  color: Colors.transparent,
                  elevation: 2,
                  highlightElevation: 0,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20)),
                  onPressed: () {
                    if (widget.priceController.text == '' ||
                        widget.nameController.text == '' ||
                        widget.numberController.text == '' ||
                        widget.describeController.text == '' ||
                        medicineImgUp.isEmpty) {
                      showDialog(
                        context: context,
                        builder: (context) => SimpleDialog(
                          title: Text('提示'),
                          children: [
                            Container(
                              margin: EdgeInsets.only(left: 10),
                              child: Text('请将内容填写完整!'),
                            ),
                            Align(
                              alignment: Alignment.centerRight,
                              child: FlatButton(
                                onPressed: () {
                                  Navigator.pop(context);
                                },
                                child: Text(
                                  '取消',
                                  style: TextStyle(color: Colors.blue),
                                ),
                              ),
                            )
                          ],
                        ),
                      );
                    } else {
                      if (widget.id == 1) {
                        _editMedicine();
                      } else {
                        _addMedicine();
                      }
                      openAlertDialog(
                          context, widget.saveInfoAndCommit, '保存成功');
                    }
                  },
                ),
              )
            ],
          ),
        )
      ],
    );
  }
}
